﻿<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/include.inc.jsp"%>
<!DOCTYPE html>
<html>
<head>
<%@include file="/WEB-INF/views/layout/base.ref.jsp"%>
<%@include file="/WEB-INF/views/layout/valid.ref.jsp"%>
<link href="${ctx}/static/controls/zTree/css/zTreeStyle.css" rel="stylesheet"
	type="text/css">
</head>
<body>
	<%@include file="/WEB-INF/views/login/home/toolbar.jsp"%>
	<div class="mtcontent">
		<div class="mtbody">
			<div class="mtrow">
				<div class="mtcol">
						<h4 class="lighter smaller">组织信息</h4>
						<ul id="menuTree" class="ztree"></ul>
				</div>
				<div class="mtcol3">

					<h4 class="lighter smaller">编辑组织信息</h4>
					<form method="post" id="form1"
						action="${ctx}/structure/sysOrganize/save">
						<input type="hidden" name="id" value="-1" /> <input type="hidden"
							name="parentId" value="-1" />
						<div class="mtheader">
							<shiro:hasPermission name="sysOrganize:add">
								<a class="mtbtn one" href="#" onclick="add()"><span><i
										class="mtfa mt-add"></i>添加</span></a>
							</shiro:hasPermission>
							<shiro:hasPermission name="sysOrganize:edit">
								<a class="mtbtn one" href="#"
									onclick="hrefSave_OnClick('form1')"><i class="mtfa mt-dui"></i><span>保存</span></a>
							</shiro:hasPermission>
							<shiro:hasPermission name="sysOrganize:del">
								<a class="mtbtn two" href="#" onclick="del()"><i
									class="mtfa mt-refresh"></i><span>删除</span></a>
							</shiro:hasPermission>
						</div>
						<div class="mtbody">
							<div class="mtrow">
								<div class="mtcol">
									<label class="mtlabel" for="organizeName">组织名称 </label> <span
										class="block mtinput mticon-input mticon-input-right">
										<input type="text" id="organizeName" name="organizeName"
										class="required" value="" /><i class="mtfa mt-star mtred"></i>
									</span>
								</div>
							</div>
							<div class="mtrow">
								<div class="mtcol">
									<label class="mtlabel">组织编码 </label> <input type="text"
										name="code" class="mtinput" value="" />
								</div>
							</div>
							<div class="mtrow">
								<div class="mtcol">
									<label class="mtlabel">显示顺序 </label> <input type="number"
										name="showOrder" class="mtinput" value="" />
								</div>
							</div>
							<div class="mtrow">
								<div class="mtcol">
									<label class="mtlabel">备注 </label> <input type="text"
										name="remark" class="mtinput" value="" />
								</div>
							</div>
						</div>
					</form>
				</div>
			</div>
		</div>
	</div>
</body>
</html>

<script src="${ctx}/static/controls/zTree/js/jquery.ztree.core.js"
	type="text/javascript"></script>
<script src="${ctx}/static/controls/zTree/js/jquery.ztree.excheck.js"
	type="text/javascript"></script>
<script src="${ctx}/static/controls/zTree/js/jquery.ztree.exedit.js"
	type="text/javascript"></script>
<script src="${ctx}/static/meeting/meeting.treeeditform.js"
	type="text/javascript"></script>
<script type="text/javascript">
	var zTree;

	$(document).ready(function() {

		var defaultsetting = {
			view : {
				fontCss : getFont,
				addDiyDom : addDiyDom
			},
			callback : {
				onClick : onClickTree,
				onDrop : zTreeOnDrop
			}
		};

		zTree = meeting.treeeditform.inittree("#menuTree", "id", "parentId", "organizeName", ${sysOrganizeList}, defaultsetting);
		$('#menuTree').height($(window).height() - 120);
		zTree.expandAll(true);

		var conodes = zTree.transformToArray(zTree.getNodes());
		for (var i = 0, l = conodes.length; i < l; i++) {
			conodes[i].font = {
				'width' : '80%',
			};
			zTree.updateNode(conodes[i]);
		}
	});

	function getFont(treeId, node) {
		return node.font ? node.font : {};
	}

	function onClickTree(e, treeId, treeNode) {
		if (treeNode.id && treeNode.id != "" && treeNode.id != "undefined") {
			$.ajax({
				async : false,
				type : "POST",
				url : "${ctx}/structure/sysOrganize/getOrganizeById",
				data : {
					id : treeNode.id
				},
				dataType : "json",
				success : function(e) {
					if (e) {
						$("input[name='id']").val(e.id);
						$("input[name='parentId']").val(e.parentId);
						$("input[name='code']").val(e.code);
						$("input[name='organizeName']").val(e.organizeName);
						$("input[name='showOrder']").val(e.showOrder);
						$("input[name='remark']").val(e.remark);
					}
				},
				error : function(e) {
					console.log(e.responseText);
				}
			});
		}
	}

	function zTreeOnDrop(event, treeId, treeNodes, targetNode, moveType) {
		for (var i = 0, l = treeNodes.length; i < l; i++) {
			UpdateSysDepartment(treeNodes[i].id, treeNodes[i].parentId);
		}
	}

	function addDiyDom(treeId, treeNode) {
		var aObj = $("#" + treeNode.tId + "_a");
		var editStr = "<span>" + treeNode.showOrder;

		editStr = editStr + "</span>";
		aObj.after(editStr);
	}
</script>

<script type="text/javascript">
	function add() {
		var nodes=zTree.getSelectedNodes();
		if(nodes.length<1){
			layer.msg("请选择一个部门！", {
				icon : 0
			});
			
			return false;
		}
		
		var node =nodes[0];
		$("input[name='id']").val("-1");
		$("input[name='parentId']").val(node.id);
		$("input[name='code']").val("");
		$("input[name='organizeName']").val("");
		$("input[name='showOrder']").val("1");
		$("input[name='remark']").val("");
	}

	function del() {
		var id = $("input[name='id']").val();
		if (id == "" || isNaN(id) || Number(id) < 1) {
			layer.msg("请选择组织！", {
				icon : 2
			});
		} else {
			var node = zTree.getNodeByParam("id", id, null);
			if (node.isParent) {
				layer.msg("该组织有下属组织，请先删除下属组织！", {
					icon : 2
				});
			} else {
				hrefOneDel_OnClick('${ctx}/structure/sysOrganize/del', id);
			}
		}
	}

	function btnAjaxCallback(result) {
		if (result != null) {
			layer.close(toolbarIndex);
			if (result.code == 'success') {
				if (result.data.id != $("input[name = 'id']").val()) {
					var parentNode = zTree.getNodeByParam("id", result.data.parentId, null);
					zTree.addNodes(parentNode, result.data);
				}
				layer.msg(result.message, {
					icon : 1
				}, function() {

				});

			} else if (result.code == 'error') {
				layer.msg(result.message, {
					icon : 2
				}, function() {
				});
			} else {
				layer.msg('连接错误', {
					icon : 2
				});
			}
		}
	}

	function btnDelAjaxCallback(result) {
		if (result != null) {
			layer.close(toolbarIndex);
			if (result.code == 'success') {
				var node = zTree.getNodeByParam("id", result.data, null);
				zTree.removeNode(node);

				$("input[name='id']").val("-1");
				$("input[name='parentId']").val("-1");
				$("input[name='code']").val("");
				$("input[name='organizeName']").val("");
				$("input[name='showOrder']").val("1");
				$("input[name='remark']").val("");

				layer.msg(result.message, {
					icon : 1
				});

			} else if (result.code == 'error') {
				layer.msg(result.message, {
					icon : 2
				});
			}
		} else {
			layer.msg('连接错误', {
				icon : 2
			});
		}
	}
</script>